package com.smartscreen.contentmanage.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.smartscreen.contentmanage.entity.EducationalContent;
import com.smartscreen.contentmanage.service.EducationalContentService;
import com.smartscreen.common.response.ApiResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/content/educational-content")
@Validated
public class EducationalContentController {
    
    @Autowired
    private EducationalContentService educationalContentService;
    
    /**
     * 创建教育内容
     * @param content 内容信息
     * @return 创建结果
     */
    @PostMapping
    public ApiResponse<Map<String, String>> createContent(@RequestBody  EducationalContent content) {
        try {
            if (content == null) {
                return ApiResponse.badRequest("内容信息不能为空");
            }
            
            boolean success = educationalContentService.save(content);
            
            if (success) {
                Map<String, String> data = new HashMap<>();
                data.put("contentId", content.getContentId());
                return ApiResponse.created("创建成功", data);
            } else {
                return ApiResponse.serverError("创建失败");
            }
        } catch (Exception e) {
            return ApiResponse.serverError("创建失败：" + e.getMessage());
        }
    }
    
    /**
     * 获取教育内容列表
     * @param category 分类（可选）
     * @return 内容列表
     */
    @GetMapping
    public ApiResponse<List<EducationalContent>> getContents(
            @RequestParam(required = false) String category) {
        
        try {
            QueryWrapper<EducationalContent> queryWrapper = new QueryWrapper<>();
            if (StringUtils.hasText(category)) {
                queryWrapper.eq("category", category);
            }
            queryWrapper.orderByDesc("created_at");
            
            List<EducationalContent> contents = educationalContentService.list(queryWrapper);
            
            if (contents == null || contents.isEmpty()) {
                return ApiResponse.success("暂无教育内容", contents);
            }
            
            return ApiResponse.success("获取教育内容成功", contents);
        } catch (Exception e) {
            return ApiResponse.serverError("获取教育内容失败：" + e.getMessage());
        }
    }
}